package com.aube.mis.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.aube.mis.dto.BaseResponse;
import com.aube.mis.enums.ResponseCode;
import com.aube.mis.exception.MisException;
import com.aube.mis.util.Constant;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.util.URLUtil;

public abstract class AbstractController {

	private static Logger log = LoggerFactory.getLogger(AbstractController.class);

	@ExceptionHandler
	public void handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
		log.error("系统异常 url=" + request.getRequestURI(), ex);

		// AJAX请求页面
		if (URLUtil.isAjaxURL(request.getRequestURI())) {
			response.setContentType("text/plain;charset=utf-8");
			BaseResponse<?> baseResp = null;
			if (ex instanceof MisException) {
				MisException misException = (MisException) ex;
				baseResp = new BaseResponse(misException.getCode(), misException.getMessage());
				response.getWriter().write(JsonUtil.toJson(baseResp));
			} else {
				response.getWriter().write(this.createResponse(ResponseCode.SYSTEM_ERROR, null, null));
			}
		}
		// 普通页面
		else {
			// 定义的站点异常, 获得信息后返回显示错误提示的页面
			if (ex instanceof MisException) {
				MisException websiteException = (MisException) ex;
				request.setAttribute("errorMsg", websiteException.getMessage());
				request.getRequestDispatcher("/error/index").forward(request, response);
			}
			// 其他异常,返回默认错误页面
			else {
				request.getRequestDispatcher("/error/index").forward(request, response);
			}
		}
	}

	protected String createSuccessResponse() {
		return this.createResponse(ResponseCode.SUCCESS, null, null);
	}

	protected String createSuccessResponse(Map<String, Object> dataMap) {
		return this.createResponse(ResponseCode.SUCCESS, dataMap, null);
	}

	protected String createSuccessResponse(String redirectUrl) {
		return this.createResponse(ResponseCode.SUCCESS, null, redirectUrl);
	}

	protected String createResponse(ResponseCode responseCode, Map<String, Object> dataMap, String redirectUrl) {
		BaseResponse<Map<String, Object>> response = new BaseResponse<Map<String, Object>>(responseCode);
		response.setData(dataMap);
		response.setRedirectUrl(redirectUrl);
		return JsonUtil.toJson(response);
	}

	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

	protected String getShowId() {
		return (String) getRequest().getAttribute(Constant.Attr.SHOW_ID);
	}
}
